﻿using BTNode = GX.BTTree.BTNode;
using BTNodeContainer = GX.BTTree.BTNodeContainer;
namespace GX
{
    /**选择一个满足条件的执行：没有则失败
    */
    public class BTNodeSelector:BTNodeContainer
    {
        BTNode _node;
        protected override void OnEnter(bool isRestart)
        {
            _node = null;
            int num = this.numChildren;
            for (int i = 0; i < num; ++i)
            {
                var node = this.GetChildAt(i);
                if (node.Evaluate())
                {
                    _node = node;
                    node.Start(this.OnChildDone);
                    return;
                }
            }
            this.Fail();
        }
        protected override void OnExit()
        {
            if (null != _node)
            {
                if (this.isInterrupt)
                    _node.Stop();
                _node = null;
            }
            base.OnExit();
        }
        override protected void OnChildDone(BTNode node)
        {
            if (this.isDone) return;
            if (node.isSuccess)
            {
                this.Finish();
            }
            else
            {
                this.Fail();
            }
        }
    }
}
